<template>
  <div class="login">
    <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">欢迎登录担保中台系统</h3>
      <el-form-item prop="username">
        <el-input v-model="loginForm.username" type="text" size="large" auto-complete="off" placeholder="账号">
          <template #prefix
            ><el-icon><User /></el-icon
          ></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="loginForm.password" type="password" size="large" auto-complete="off" placeholder="密码" @keyup.enter="handleLogin" show-password>
          <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaEnabled">
        <div class="login-code">
          <el-input v-model="loginForm.code" size="large" auto-complete="off" placeholder="验证码" style="width: 50%" @keyup.enter="handleLogin">
            <template #prefix><svg-icon icon-class="validCode" class="el-input__icon input-icon" /></template>
          </el-input>
          <img :src="codeUrl" />
          <div class="btn" @click="getCode">
            <el-icon><RefreshLeft /></el-icon>
          </div>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button :loading="loading" size="large" type="primary" style="width: 100%" @click.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
    <div class="el-login-footer">
      <span>Copyright © 2023 上海天畅信息技术公司, All Rights Reserved.</span>
    </div>
  </div>
</template>

<script setup>
import { getCodeImg, addAccount } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import useUserStore from "@/store/modules/user";

const userStore = useUserStore();
const router = useRouter();
const { proxy } = getCurrentInstance();

const loginForm = ref({
  username: "",
  password: "",
  code: "",
  uuid: "",
});

const loginRules = {
  username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }],
};

const codeUrl = ref("");
const loading = ref(false);
// 验证码开关
const captchaEnabled = ref(true);
const redirect = ref(undefined);

function handleLogin() {
  proxy.$refs.loginRef.validate((valid) => {
    if (valid) {
      loading.value = true;
      // 调用action的登录方法
      userStore
        .login(loginForm.value)
        .then(() => {
          router.push({ path: redirect.value || "/" });
        })
        .catch((err) => {
          loading.value = false;
          // 重新获取验证码
          if (captchaEnabled.value) {
            getCode();
          }
        });
    }
  });
}
function getCode() {
  getCodeImg().then((res) => {
    codeUrl.value = "data:image/png;base64," + btoa(new Uint8Array(res.data).reduce((data, byte) => data + String.fromCharCode(byte), ""));
    // captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled;
    // if (captchaEnabled.value) {
    //   codeUrl.value = "data:image/gif;base64," + res.img;
    //   loginForm.value.uuid = res.uuid;
    // }
  });
}

function getCookie() {
  const username = Cookies.get("username");
  const password = Cookies.get("password");
  loginForm.value = {
    username: username === undefined ? loginForm.value.username : username,
    password: password === undefined ? loginForm.value.password : decrypt(password),
  };
}
getCode();
getCookie();
</script>

<style lang="scss" scoped>
.login {
  display: flex;
  height: 100%;
  background-image: url("../assets/images/bj.png");
  background-size: cover;
  position: relative;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  font-family: Source Han Sans CN;
  font-weight: 800;
  color: #333333;
}

.login-form {
  width: 441px;
  height: 320px;
  border-radius: 10px;
  position: absolute;
  top: 35%;
  left: 60%;
  border-radius: 6px;
  background: #ffffff;
  padding: 25px;

  // .title {
  //   padding-bottom: 8vh;
  //   text-align: center;
  // }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;
  }

  // .el-button {
  //   margin-top: 8vh;
  // }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  display: flex;
  width: 100%;
  align-items: center;
  justify-content: space-between;
  .btn {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 25px;
    height: 25px;
    font-size: 18px;
    font-weight: bold;
    color: white;
    background-color: black;
    border-radius: 50%;
  }
}
.el-button {
  border: none;
  background-image: linear-gradient(0.25turn, rgba(0, 0, 255, 0), rgba(0, 0, 255, 1));
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
</style>
